#include "bsp.h"

unsigned char g_uart2_rx_buf[USART2_RX_SIZE];
BspUart_t g_tBspUart2= 
{
	.pRxBuf = g_uart2_rx_buf,
	.rxSize = USART2_RX_SIZE,
};

#ifdef ENABLE_RTT_FUNCTIONS
// SEGGER_RTT_CustomFunction ÊµÏÖ

/// @brief ·â×°Seggerº¯Êý
/// @param TerminalId 	Êä³ö´°¿Ú	
/// @param BufferIndex 	
/// @param sFormat 		¿É±ä²ÎÊý
/// @param  
/// @return 
int SEGGER_RTT_CustomFunction( char TerminalId , unsigned BufferIndex, const char *sFormat, ...) {
    va_list ParamList;

    // ³õÊ¼»¯¿É±ä²ÎÊýÁÐ±í
    va_start(ParamList, sFormat);
		SEGGER_RTT_SetTerminal(TerminalId);
    // Ê¹ÓÃ SEGGER_RTT_vprintf À´´¦Àí va_list ÀàÐÍµÄ²ÎÊý
    int r = SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);

    // ½áÊø¿É±ä²ÎÊýÁÐ±í
    va_end(ParamList);

    // Èç¹û SEGGER_RTT_vprintf ³É¹¦£¬¼ÌÐøµ÷ÓÃ SEGGER_RTT_SetTerminal
    if (r >= 0) {
        r = SEGGER_RTT_SetTerminal(TerminalId);  // ÉèÖÃÖÕ¶Ë
    }

    return r;
}
#endif // ENABLE_RTT_FUNCTIONS

void PWMOutputSwitch(unsigned char num)
{
	switch(num)
	{
		case PWMCHA://tag 	
			HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
			HAL_TIM_PWM_Stop(&htim15 , TIM_CHANNEL_2);
			HAL_TIM_PWM_Stop(&htim15 , TIM_CHANNEL_1);
			HAL_GPIO_WritePin(ENA_GPIO_Port, ENA_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(ENB_GPIO_Port, ENB_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(ENC_GPIO_Port, ENC_Pin, GPIO_PIN_RESET);					
			break;
		
		case PWMCHB://tag
			HAL_TIM_PWM_Start(&htim15 , TIM_CHANNEL_1);
			HAL_TIM_PWM_Stop(&htim15 , TIM_CHANNEL_2);	
			HAL_TIMEx_PWMN_Stop(&htim1 , TIM_CHANNEL_1);
			HAL_GPIO_WritePin(ENA_GPIO_Port, ENA_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(ENB_GPIO_Port, ENB_Pin, GPIO_PIN_SET);
			HAL_GPIO_WritePin(ENC_GPIO_Port, ENC_Pin, GPIO_PIN_RESET);		
			break;
		
		case PWMCHC://tag 
			HAL_TIM_PWM_Start(&htim15 , TIM_CHANNEL_2);
			HAL_TIM_PWM_Stop(&htim15 , TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Stop(&htim1 , TIM_CHANNEL_1);
			HAL_GPIO_WritePin(ENA_GPIO_Port, ENA_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(ENB_GPIO_Port, ENB_Pin, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(ENC_GPIO_Port, ENC_Pin, GPIO_PIN_SET);		
			break;		
	}
}

/**
 * @brief Prints a float value via SEGGER RTT with a specified precision.
 * @param BufferIndex RTT buffer index (usually 0).
 * @param pPrefix String to print before the number.
 * @param fValue The float value to print.
 * @param Precision Number of digits after the decimal point.
 * @param pSuffix String to print after the number (e.g., "\r\n").
 */
void RTT_printf_float(unsigned BufferIndex, const char* pPrefix, float fValue, unsigned int Precision, const char* pSuffix)
{
    long long intPart;
    long long fracPart;
    unsigned int i, Divisor;
    char sign = ' ';

    if (fValue < 0) {
        sign = '-';
        fValue = -fValue;
    }

    Divisor = 1;
    for (i = 0; i < Precision; i++) {
        Divisor *= 10;
    }

    intPart = (long long)fValue;
    fracPart = (long long)((fValue - intPart) * Divisor + 0.5); // Add 0.5 for rounding

    // Handle rollover from rounding
    if (fracPart >= Divisor) {
        fracPart -= Divisor;
        intPart++;
    }
    
    SEGGER_RTT_printf(BufferIndex, "%s%c%d.", pPrefix, sign, intPart);
    
    // Print leading zeros for the fractional part
    long long temp = fracPart;
    unsigned int digits = 0;
    if (temp == 0) {
        digits = 1;
    } else {
        while(temp > 0) {
            temp /= 10;
            digits++;
        }
    }
    for (i = 0; i < (Precision - digits); i++) {
        SEGGER_RTT_printf(BufferIndex, "0");
    }

    if (fracPart > 0) 
	{
       SEGGER_RTT_printf(BufferIndex, "%d", fracPart);
    }
	else if (Precision > 0 && digits <= Precision) 
	{
       // if fracPart was 0, but precision requires digits, print nothing here as leading zeros are already printed.
    }
    SEGGER_RTT_printf(BufferIndex, "%s", pSuffix);
}


